package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.common.DateUtil;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.mapper.SaleinfoMapper;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Saleinfo;
import com.wkw.jkyf.service.ISaleInfoService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SaleinfoServiceImpl  extends ServiceImpl<SaleinfoMapper, Saleinfo> implements ISaleInfoService {

    @Resource
    private SaleinfoMapper SaleinfoMapper;
    @Resource
    private DruginfoMapper druginfoMapper;


    @Override
    public IPage<Saleinfo> selectSaleinfoPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Saleinfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Saleinfo> page = new Page<>(pageNum,pageSize);
        return SaleinfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    @Transactional
    public int addSaleinfo(Saleinfo saleinfo) {
        int operateFlag = 0;
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",saleinfo.getDname())
                .eq("number",saleinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品库存数量
        int stock = druginfo.getStock();
        //销售数量
        int saleCount = saleinfo.getCount();
        if(saleCount>stock){
            return -1;
        }
        druginfo.setStock(stock-saleCount);
        operateFlag += druginfoMapper.updateById(druginfo);
        //得到当前用户对象
        Subject subject = SecurityUtils.getSubject();
        String userName = (String)subject.getSession().getAttribute("userName");
        saleinfo.setOperator(userName);
        saleinfo.setOperatetime(new Date(System.currentTimeMillis()));
        Float price = druginfo.getPrice();
        float resultTotal = saleCount * price;
        saleinfo.setTotal(Double.valueOf(resultTotal+""));
        operateFlag += SaleinfoMapper.insert(saleinfo);
        System.out.println("update" + operateFlag);
        return operateFlag;
    }


    @Override
    @Transactional
    //修改销售记录
    public int editSaleinfo(Saleinfo saleinfo) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",saleinfo.getDname())
                .eq("number",saleinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);

        QueryWrapper<Saleinfo> saleinfoWrapper = new QueryWrapper<>();
        saleinfoWrapper
                .eq("id",saleinfo.getId());
        //当前数据库的销售信息
        Saleinfo oldSaleInfo = SaleinfoMapper.selectOne(saleinfoWrapper);
        if(!oldSaleInfo.getDnumber().equals(saleinfo.getDnumber())){
           return -2;
        }
        //当前数据库的销售数量
        int oldCount = oldSaleInfo.getCount();
        //新的销售数量
        int newCount = saleinfo.getCount();
        if(newCount<oldCount){
            druginfo.setStock(druginfo.getStock()+(oldCount-newCount));
            druginfoMapper.updateById(druginfo);
        }else if(newCount>oldCount){
            int newSaleCount = newCount - oldCount;
            if(newSaleCount>druginfo.getStock()){
                return -1;
            }
            druginfo.setStock(druginfo.getStock()-newSaleCount);
            druginfoMapper.updateById(druginfo);
        }

        //总金额
        float finalTotal = saleinfo.getCount() * druginfo.getPrice();
        saleinfo.setTotal(Double.valueOf(finalTotal+""));
        saleinfo.setOperatetime(new Date());
        return SaleinfoMapper.updateById(saleinfo);
    }

    @Override
    public Saleinfo querySaleinfoById(Integer id) {
        return SaleinfoMapper.selectById(id);
    }

    @Override
    @Transactional
    public int deleteSaleinfoById(Integer id) {
         Saleinfo saleinfo = SaleinfoMapper.selectById(id);
         //查询出来的数量
         Integer count = saleinfo.getCount();
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",saleinfo.getDname())
                .eq("number",saleinfo.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        druginfo.setStock(druginfo.getStock()+count);
        druginfoMapper.updateById(druginfo);
        return SaleinfoMapper.deleteById(id);
    }
}
